package com.ztorn.utils;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * @description 请求类
 * */
public class ScHttpRequestUtil {

	public static String postHttpurlconnection(String url,String data){
		HttpURLConnection connection = null;
		try {
			URL u = new URL(url);
			connection = (HttpURLConnection) u.openConnection();
			connection.setRequestMethod("POST");
			connection.setRequestProperty("Content-Type", "application/json; utf-8");
			connection.setRequestProperty("Accept", "application/json");
			connection.setDoOutput(true);
			connection.setDoInput(true);
//			connection.setRequestProperty("version", version);
//			connection.setRequestProperty("authenticate", authenticate);
			OutputStream os = connection.getOutputStream();
			os.write(data.getBytes("UTF-8"));
			System.out.println(connection.getResponseCode()+"!!!!!!!!!!!!!!");
			if(connection.getResponseCode()==200){
				InputStream is = connection.getInputStream();
				BufferedReader bf=new BufferedReader(new InputStreamReader(is,"UTF-8")); 
				StringBuffer buffer=new StringBuffer();  
				String line="";  
				while((line=bf.readLine())!=null){
				    buffer.append(line);  
				}
			    return buffer.toString();
			}
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			connection.disconnect();
		}
		return null;
	}

	
	/**
	 * Http Get请求
	 * */
	public static String getHttpurlconnection(String url){
		HttpURLConnection connection = null;
		try {
			URL u = new URL(url);
			connection = (HttpURLConnection) u.openConnection();
			connection.setRequestMethod("GET");
			connection.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");
//			connection.setRequestProperty("version", version);
//			connection.setRequestProperty("authenticate", authenticate);
			InputStream is = connection.getInputStream();
			BufferedReader bf=new BufferedReader(new InputStreamReader(is,"UTF-8")); 
			StringBuffer buffer=new StringBuffer();  
			String line="";  
			while((line=bf.readLine())!=null){
			    buffer.append(line);  
			}
		    return buffer.toString();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			connection.disconnect();
		}
		return null;
	}
	
	/**
	 * Http Get获取中邮跟踪信息请求
	 * */
	public static String getYZTracks(String url,String authenticate,String version){
		HttpURLConnection connection = null;
		try {
			URL u = new URL(url);
			connection = (HttpURLConnection) u.openConnection();
			connection.setRequestMethod("GET");
			connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
			connection.setRequestProperty("version", version);
			connection.setRequestProperty("authenticate", authenticate);
			InputStream is = connection.getInputStream();
			BufferedReader bf=new BufferedReader(new InputStreamReader(is,"UTF-8")); 
			StringBuffer buffer=new StringBuffer();  
			String line="";  
			while((line=bf.readLine())!=null){
			    buffer.append(line);  
			}
		    return buffer.toString();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			connection.disconnect();
		}
		return null;
	}
	
}

